/**
 * Light-commons Project
 * http://light-commons.googlecode.com
 * Copyright (C) 2008 Jason Green
 * email: guileen@gmail.com
 *
 * License: Apache License 2.0 
 * (http://www.apache.org/licenses/LICENSE-2.0)
 *
 */
package org.lightcommons.template.commontemplate;

import java.io.IOException;
import java.io.InputStream;

import org.commontemplate.core.ParsingException;
import org.commontemplate.core.Resource;
import org.commontemplate.engine.Engine;
import org.commontemplate.engine.template.TemplateEngine;
import org.commontemplate.standard.ConfigurationSettings;
import org.commontemplate.standard.loader.AbstractResource;
import org.commontemplate.standard.loader.AbstractResourceLoader;
import org.commontemplate.standard.loader.StringResource;
import org.commontemplate.tools.PropertiesConfigurationLoader;
import org.lightcommons.resource.ResourceLoader;
import org.lightcommons.template.AbstractTemplateFactory;
import org.lightcommons.template.Template;
import org.lightcommons.util.Assert;

/**
 * @author gl
 * @since Jun 16, 2008
 */
public class CommonTemplateFactory extends AbstractTemplateFactory{
	private ConfigurationSettings config;
//	private TemplateParser templateParser;
//	private ContextFactory contextFactory;
	private Engine engine;
	private TemplateEngine templateEngine;
	public static final String STANDARD_CONFIG_PATH=PropertiesConfigurationLoader.STANDARD_CONFIG_PATH;
	public static final String WEB_CONFIG_PATH="org/commontemplate/tools/web/commontemplate.properties";
	
	public CommonTemplateFactory(ConfigurationSettings config) {
		init(config);
	}
	
	private void init(ConfigurationSettings config){
		this.config = config;
		this.engine = new Engine(config);
		this.templateEngine = new TemplateEngine(config);
	}
	
	/**
	 * 
	 */
	public CommonTemplateFactory() {
		this(PropertiesConfigurationLoader.loadStandardConfiguration());
	}
	
	/**
	 * 
	 * @param propertiesPath
	 */
	public CommonTemplateFactory(String propertiesPath){
		this(PropertiesConfigurationLoader.loadConfiguration(propertiesPath));
	}

	@Override
	public Template compileSource(String name, String source,String encoding) throws ParsingException, IOException {
		return new CommonTemplateFacade(templateEngine.parseTemplate(new StringResource(name,source)),engine);
	}

	@Override
	public void setResourceLoader(ResourceLoader loader) {
		super.setResourceLoader(loader);
		config.setResourceLoader(new CommonTemplateLoader(loader));
		init(config);
	}
	
	private static class CommonTemplateResource extends AbstractResource{
		private org.lightcommons.resource.Resource resource;
		
		public CommonTemplateResource(org.lightcommons.resource.Resource resource,String name,String encoding) {
			super(name, encoding);
			Assert.notNull(resource);
			this.resource = resource;
		}

		@Override
		protected InputStream getInputStream() throws IOException {
			return resource.getInputStream();
		}

		@Override
		public long getLastModified() {
			return 0;
		}
		
	}
	
	private static class CommonTemplateLoader extends AbstractResourceLoader{
		private ResourceLoader loader;
		
		public CommonTemplateLoader(ResourceLoader loader) {
			super();
			this.loader = loader;
		}

		@Override
		protected Resource loadResource(String path, String name,
				String encoding) throws IOException {
			org.lightcommons.resource.Resource resource = loader.getResource(path);
			Assert.notNull(resource,"can't load resource of '"+path+"'");
			return new CommonTemplateResource(resource,name,encoding);
		}
		
	}
}
